package com.secxiot.provider.narcotic.service.impl;

import com.itextpdf.text.DocumentException;
import com.secxiot.common.constant.ServerStatusEnum;
import com.secxiot.common.entity.ResultVo;
import com.secxiot.common.exception.BaseRuntimeException;
import com.secxiot.common.utils.CachedBeanCopier;
import com.secxiot.common.utils.JSMSUtil;
import com.secxiot.common.utils.SignWriteToPDF;
import com.secxiot.config.FileConfig;
import com.secxiot.consumer.user.feign.UserClient;
import com.secxiot.provider.narcotic.dao.*;
import com.secxiot.provider.narcotic.dto.NarcoticPatientDrugsRecordDto;
import com.secxiot.provider.narcotic.entity.*;
import com.secxiot.provider.narcotic.service.NarcoticService;
import com.secxiot.provider.narcotic.vo.*;
import com.secxiot.provider.system.vo.FileLogVo;
import com.secxiot.provider.user.dto.UserDto;
import com.secxiot.provider.user.entity.UserDo;
import com.secxiot.provider.user.vo.UserSimpleVo;
import org.apache.commons.lang.time.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestParam;

import java.io.File;
import java.io.IOException;
import java.util.*;
import java.util.stream.Collectors;

@Transactional
@Service
public class NarcoticServiceImpl implements NarcoticService {

    private static Logger log = LoggerFactory.getLogger(NarcoticServiceImpl.class);

    @Autowired
    private NarcoticDocMapper narcoticDocMapper;

    @Autowired
    private NarcoticDrugsMapper narcoticDrugsMapper;

    @Autowired
    private NarcoticPatientInfoMapper narcoticPatientInfoMapper;

    @Autowired
    private NarcoticPatientDrugsRecordMapper narcoticDrugsRecordMapper;

    @Autowired
    private NarcoticDoctorRecordMapper narcoticDoctorRecordMapper;

    @Autowired
    private NarcoticChemistRecordMapper narcoticChemistRecordMapper;

    @Autowired
    private NarcoticTakeDestroyMapper narcoticTakeDestroyMapper;

    @Autowired
    private NarcoticAgentInfoMapper narcoticAgentInfoMapper;

    @Autowired
    private NarcoticDrugsFactoryMapper narcoticDrugsFactoryMapper;


    @Autowired
    private UserClient userClient;

    @Autowired
    private FileConfig fileConfig;

    @Override
    public NarcoticDocDo selectNarcoticDoc(Long hospitalId, String type) {
        return narcoticDocMapper.selectByMap(hospitalId, type);
    }

    @Override
    public List<NarcoticDrugsVo> selectNarcoticDrugs(Long hospitalId) {
        List<NarcoticDrugsDo> narcoticDrugsTypeDos = narcoticDrugsMapper.selectNarcoticDrugs(hospitalId);
        List<NarcoticDrugsVo> narcoticDrugsTypeVoList = narcoticDrugsTypeDos.stream().map(p -> {
            NarcoticDrugsVo narcoticDrugsTypeVo = new NarcoticDrugsVo();
            CachedBeanCopier.copy(p, narcoticDrugsTypeVo);
            return narcoticDrugsTypeVo;
        }).collect(Collectors.toList());
        return narcoticDrugsTypeVoList;
    }

    @Override
    public NarcoticDrugsVo getNarcoticDrugsById(Long drugsId) {
        NarcoticDrugsDo drugsDo = narcoticDrugsMapper.getNarcoticDrugsById(drugsId);
        NarcoticDrugsVo narcoticDrugsVo = new NarcoticDrugsVo();
        CachedBeanCopier.copy(drugsDo, narcoticDrugsVo);
        return narcoticDrugsVo;
    }

    @Override
    public List<NarcoticPatientInfoVo> selectNarcoticPatientInfoList(Long userId) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("userId", userId);
        return tranPatientInfoVo(map);
    }

    @Override
    public List<NarcoticPatientInfoVo> selectNarcoticPatientInfoPage(Long userId, Integer pageNo, Integer pageSize) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("userId", userId);
        map.put("pageNo", pageNo * pageSize);
        map.put("pageSize", pageSize);
        map.put("isPage", 1);
        return tranPatientInfoVo(map);
    }

    public List<NarcoticPatientInfoVo> tranPatientInfoVo(Map<String, Object> map) {
        List<NarcoticPatientInfoDo> narcoticUserInfoDos = narcoticPatientInfoMapper.selectNarcoticPatientInfoDo(map);
        List<NarcoticPatientInfoVo> narcoticUserInfoVos = narcoticUserInfoDos.stream().map(p -> {
            NarcoticPatientInfoVo narcoticUserInfoVo = new NarcoticPatientInfoVo();
            CachedBeanCopier.copy(p, narcoticUserInfoVo);
            return narcoticUserInfoVo;
        }).collect(Collectors.toList());
        return narcoticUserInfoVos;
    }

    @Override
    public NarcoticPatientInfoVo getNarcoticPatientInfoById(Long patientInfoId) {
        NarcoticPatientInfoVo patientInfoVo = new NarcoticPatientInfoVo();
        NarcoticPatientInfoDo patientInfoDo = narcoticPatientInfoMapper.getNarcoticPatientInfoById(patientInfoId);
        if (patientInfoDo == null) {
            return null;
        }
        CachedBeanCopier.copy(patientInfoDo, patientInfoVo);

        //患者身份证附件
        if (patientInfoVo.getIdCardFile() != null) {
            List<FileLogVo> fileLogList = narcoticPatientInfoMapper.selectFileByCode(patientInfoVo.getIdCardFile());
            if (fileLogList != null) {
                patientInfoVo.setIdCardFileList(fileLogList);
            }
        }
        //代领人身份证附件
        if (patientInfoVo.getAgentIdCardFile() != null) {
            List<FileLogVo> fileLogVoList = narcoticPatientInfoMapper.selectFileByCode(patientInfoVo.getAgentIdCardFile());
            if (fileLogVoList != null) {
                patientInfoVo.setAgentIdCardFileList(fileLogVoList);
            }
        }
        //病历附件
        if (patientInfoVo.getPatientCaseFile() != null) {
            List<FileLogVo> fileLogList = narcoticPatientInfoMapper.selectFileByCode(patientInfoVo.getPatientCaseFile());
            if (fileLogList != null) {
                patientInfoVo.setPatientCaseFileList(fileLogList);
            }
        }
        //诊断证明附件
        if (patientInfoVo.getDiagnoseProveFile() != null) {
            List<FileLogVo> fileLogList = narcoticPatientInfoMapper.selectFileByCode(patientInfoVo.getDiagnoseProveFile());
            if (fileLogList != null) {
                patientInfoVo.setDiagnoseProveFileList(fileLogList);
            }
        }
        //患者照片附件
        if (patientInfoVo.getPatientPhotoFile() != null) {
            List<FileLogVo> fileLogList = narcoticPatientInfoMapper.selectFileByCode(patientInfoVo.getPatientPhotoFile());
            if (fileLogList != null) {
                patientInfoVo.setPatientPhotoFileList(fileLogList);
            }
        }
        //患者视频附件
        if (patientInfoVo.getPatientVideoFile() != null) {
            List<FileLogVo> fileLogList = narcoticPatientInfoMapper.selectFileByCode(patientInfoVo.getPatientVideoFile());
            if (fileLogList != null) {
                patientInfoVo.setPatientVideoFileList(fileLogList);
            }
        }

        //患者签名附件
        if (patientInfoVo.getSignatureFile() != null) {
            List<FileLogVo> fileLogList = narcoticPatientInfoMapper.selectFileByCode(patientInfoVo.getSignatureFile());
            if (fileLogList != null) {
                patientInfoVo.setSignatureFileList(fileLogList);
            }
        }
        return patientInfoVo;
    }

    /**
     * 添加用户资料信息
     *
     * @param narcoticPatientInfoVo 资料对象信息
     * @return int
     */
    @Override
    public Long insertNarcoticPatientInfo(NarcoticPatientInfoVo narcoticPatientInfoVo) {
        List<NarcoticPatientInfoVo> patientInfoList = narcoticPatientInfoMapper.getPatientInfoByUserId(narcoticPatientInfoVo.getUserId());
        if (!CollectionUtils.isEmpty(patientInfoList)) {
            throw new BaseRuntimeException(ServerStatusEnum.NARCOTIC_INFO_SUBMIT);
        }

        String pdf = "/user_sign/";
        String filePath = fileConfig.getUpload();
        File f = new File(filePath);
        if (!f.getParentFile().exists()) {
            f.getParentFile().mkdirs();
        }

        List<FileLogVo> fileLogList = narcoticPatientInfoMapper.selectFileByCode(narcoticPatientInfoVo.getSignatureFile());
        FileLogVo fileLogVo = fileLogList.get(0);
        String outPdf = filePath + pdf + fileLogVo.getCode() + ".pdf";
        //放置签名
        try {
            SignWriteToPDF.setWaterPrint(outPdf, filePath + pdf + "spatient_sign.pdf", filePath + fileLogVo.getFileUri());
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        String outImg = pdf + fileLogVo.getCode() + ".png";
        //转成png
        SignWriteToPDF.pdfTOimg(outPdf, filePath + outImg, "png");
        SignWriteToPDF.deleteFile(outPdf);
        narcoticPatientInfoVo.setSignatureFile(outImg);

        //添加资料信息
        NarcoticPatientInfoDo narcoticPatientInfoDo = new NarcoticPatientInfoDo();
        CachedBeanCopier.copy(narcoticPatientInfoVo, narcoticPatientInfoDo);
        narcoticPatientInfoDo.setCreateTime(new Date());
        narcoticPatientInfoDo.setIsAudit(0);
        narcoticPatientInfoDo.setStatus(1);
        Long id = narcoticPatientInfoMapper.insert(narcoticPatientInfoDo);

        //修改用户信息
        UserDto userDto = UserDto.builder()
                .userId(narcoticPatientInfoVo.getUserId())
                .username(narcoticPatientInfoVo.getUserName())
                .liveAddress(narcoticPatientInfoVo.getAddress())
                .identificationNumber(narcoticPatientInfoVo.getIdNumber())
                .sex(narcoticPatientInfoVo.getSex())
                .age(narcoticPatientInfoVo.getAge())
                .build();
        userClient.update(userDto);

        // 代领人信息
        NarcoticAgentInfoDo narcoticAgentInfoDo = new NarcoticAgentInfoDo();
        CachedBeanCopier.copy(narcoticPatientInfoVo, narcoticAgentInfoDo);
        narcoticAgentInfoDo.setCreateTime(new Date());
        //narcoticAgentInfoDo.setUserId(narcoticPatientInfoVo.getUserId());
        narcoticAgentInfoDo.setHospitalId(narcoticPatientInfoVo.getHospitalId());
        narcoticAgentInfoDo.setCreateUser(narcoticPatientInfoVo.getUserId());
        narcoticAgentInfoDo.setPatientInfoId(narcoticPatientInfoDo.getPatientInfoId());
        narcoticAgentInfoMapper.insert(narcoticAgentInfoDo);
        return id;
    }

    @Override
    public Long updateNarcoticPatientInfo(NarcoticPatientInfoVo narcoticPatientInfoVo) {
        NarcoticPatientInfoDo narcoticPatientInfoDo = new NarcoticPatientInfoDo();
        CachedBeanCopier.copy(narcoticPatientInfoVo, narcoticPatientInfoDo);
        return narcoticPatientInfoMapper.updateByPrimaryKey(narcoticPatientInfoDo);
    }

    @Override
    public List<NarcoticPatientDrugsRecordVo> selectNarcoticPatientDrugsRecord(Long userId, Integer pageNo, Integer pageSize, Integer isPage, String searchName) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("userId", userId);
        if (isPage == 1) {
            map.put("pageNo", pageNo * pageSize);
            map.put("pageSize", pageSize);
        }
        map.put("isPage", isPage);
        map.put("searchName", searchName);
        List<NarcoticPatientDrugsRecordDo> narcoticDrugsRecordDos = narcoticDrugsRecordMapper.selectNarcoticPatientDrugsRecord(map);
        List<NarcoticPatientDrugsRecordVo> narcoticDrugsRecordVos = narcoticDrugsRecordDos.stream().map(p -> {
            NarcoticPatientDrugsRecordVo narcoticDrugsRecordVo = new NarcoticPatientDrugsRecordVo();
            CachedBeanCopier.copy(p, narcoticDrugsRecordVo);
            return narcoticDrugsRecordVo;
        }).collect(Collectors.toList());
        return narcoticDrugsRecordVos;
    }

    @Override
    public Long insertNarcoticDrugsRecord(NarcoticPatientDrugsRecordVo patientDrugsRecordVo) {
        List<NarcoticPatientDrugsRecordVo> drugsRecordList = narcoticDrugsRecordMapper.getPatientDrugsRecordByUserId(patientDrugsRecordVo.getUserId(), patientDrugsRecordVo.getDrugsId());
        if (!CollectionUtils.isEmpty(drugsRecordList)) {
            throw new BaseRuntimeException(ServerStatusEnum.NARCOTIC_DRUGS_RECORD);
        }

        NarcoticPatientInfoDo narcoticPatientInfo = narcoticPatientInfoMapper.getNarcoticPatientInfoById(patientDrugsRecordVo.getPatientInfoId());
        if (narcoticPatientInfo.getIsAudit() == 0) {
            throw new BaseRuntimeException(ServerStatusEnum.NARCOTIC_INFO_NOT_AUDIT);
        }

        if (narcoticPatientInfo.getIsAudit() == 2) {
            throw new BaseRuntimeException(ServerStatusEnum.NARCOTIC_INFO_REJECT);
        }

        if (narcoticPatientInfo.getValidTime().before(new Date())) {
            throw new BaseRuntimeException(ServerStatusEnum.NARCOTIC_INFO_EXPIRE);
        }

        //1.添加患者申请用药记录
        NarcoticPatientDrugsRecordDo patientDrugsRecordDo = new NarcoticPatientDrugsRecordDo();
        CachedBeanCopier.copy(patientDrugsRecordVo, patientDrugsRecordDo);
        patientDrugsRecordDo.setCreateTime(new Date());
        patientDrugsRecordDo.setStatus(0);
        Long drugsRecordId = narcoticDrugsRecordMapper.insert(patientDrugsRecordDo);

        NarcoticDoctorRecordDo doctorRecordDo = narcoticDoctorRecordMapper.selectDrugsRecordByUser(patientDrugsRecordVo.getHospitalId(), patientDrugsRecordVo.getUserId());
        //2.添加医生开处方记录
        NarcoticDoctorRecordDo narcoticDoctorRecordDo = new NarcoticDoctorRecordDo();
        narcoticDoctorRecordDo.setCreateTime(new Date());
        narcoticDoctorRecordDo.setHospitalId(patientDrugsRecordDo.getHospitalId());
        narcoticDoctorRecordDo.setIsAudit(0);
        narcoticDoctorRecordDo.setPatientDrugsRecordId(patientDrugsRecordDo.getPatientDrugsRecordId());
        narcoticDoctorRecordDo.setDrugsId(patientDrugsRecordDo.getDrugsId());
        narcoticDoctorRecordDo.setPatientInfoId(patientDrugsRecordDo.getPatientInfoId());
        narcoticDoctorRecordDo.setPatientUserId(patientDrugsRecordDo.getUserId());
        if (null != doctorRecordDo) {
            narcoticDoctorRecordDo.setClinicalDiagnosis(doctorRecordDo.getClinicalDiagnosis());
        }
        narcoticDoctorRecordMapper.insert(narcoticDoctorRecordDo);

        NarcoticAgentInfoDo narcoticAgentInfoDo = narcoticAgentInfoMapper.selectNarcoticAgentInfo(patientDrugsRecordDo.getPatientInfoId());
        List<UserSimpleVo> drugsDeptUserList = narcoticDrugsMapper.getDrugsDeptUser(patientDrugsRecordDo.getDrugsId(), patientDrugsRecordDo.getHospitalId());
        if (!CollectionUtils.isEmpty(drugsDeptUserList)) {
            for (UserSimpleVo userSimpleVo : drugsDeptUserList) {
                try {
                    UserDo user = userClient.getUser(patientDrugsRecordVo.getUserId());
                    Map<String, String> smsMap = new HashMap<String, String>();
                    smsMap.put("name", user.getUsername());
                    smsMap.put("sex", (user.getSex() > 0) ? "男" : "女");
                    smsMap.put("phone", user.getMobile());
                    smsMap.put("identity_id", user.getIdentificationNumber());
                    smsMap.put("agent_name", (narcoticAgentInfoDo == null) ? "本人" : narcoticAgentInfoDo.getAgentName());
                    JSMSUtil.sendTempSMS(userSimpleVo.getMobile(), 183522, smsMap);
                } catch (Exception e) {
                    log.error(e.getMessage(), e);
                }
            }
        }
        return drugsRecordId;
    }

    @Override
    public Long updateNarcoticDrugsRecord(NarcoticPatientDrugsRecordVo patientDrugsRecordVo) {
        NarcoticPatientDrugsRecordDo patientDrugsRecordDo = new NarcoticPatientDrugsRecordDo();
        CachedBeanCopier.copy(patientDrugsRecordVo, patientDrugsRecordDo);
        patientDrugsRecordDo.setStatus(0);
        return narcoticDrugsRecordMapper.updateByPrimaryKey(patientDrugsRecordDo);
    }

    @Override
    public NarcoticDoctorRecordVo getNarcoticDoctorRecordById(Long doctorRecordId) {
        NarcoticDoctorRecordDo narcoticDoctorRecordDo = narcoticDoctorRecordMapper.getNarcoticDoctorRecordById(doctorRecordId);
        NarcoticDoctorRecordVo narcoticDoctorRecordVo = new NarcoticDoctorRecordVo();
        CachedBeanCopier.copy(narcoticDoctorRecordDo, narcoticDoctorRecordVo);
        return narcoticDoctorRecordVo;
    }

    /**
     * 医生药品记录 列表
     *
     * @param doctorUserId 医生用户ID
     * @param hospitalId   医疗机构ID
     * @param pageNo       页数
     * @param pageSize     条数
     * @param isPage       是否分页  0 否  1 是
     * @param searchName   关键字
     * @return
     */
    @Override
    public List<NarcoticDoctorRecordVo> selectNarcoticDoctorRecordList(Long doctorUserId, Long hospitalId, Integer pageNo, Integer pageSize, Integer isPage, String searchName) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("doctorUserId", doctorUserId);
        map.put("hospitalId", hospitalId);
        if (isPage == 1) {
            map.put("pageNo", pageNo * pageSize);
            map.put("pageSize", pageSize);
        }
        map.put("isPage", isPage);
        map.put("searchName", searchName);
        List<NarcoticDoctorRecordDo> narcoticDoctorRecordDos = narcoticDoctorRecordMapper.selectNarcoticDoctorRecord(map);
        List<NarcoticDoctorRecordVo> narcoticDoctorRecordVos = narcoticDoctorRecordDos.stream().map(p -> {
            NarcoticDoctorRecordVo narcoticDoctorRecordVo = new NarcoticDoctorRecordVo();
            CachedBeanCopier.copy(p, narcoticDoctorRecordVo);
            return narcoticDoctorRecordVo;
        }).collect(Collectors.toList());
        return narcoticDoctorRecordVos;
    }

    /**
     * 审核 药品记录
     * 修改申请药品记录 状态 和 审核时间
     * 修改医生药品记录 状态 和 审核时间 和 医生用户ID  剂量个数 医生备注 审核时间 医生拒绝原因
     * 1 审核通过
     * 添加药师药品记录信息
     * 2 审核拒绝
     * 添加拒绝原因
     */
    @Override
    public ResultVo<NarcoticDoctorRecordVo> auditNarcoticDoctorRecord(NarcoticDoctorRecordVo narcoticDoctorRecordVo) {
        Date date = new Date();
        NarcoticDoctorRecordDo narcoticDoctorRecordDo = new NarcoticDoctorRecordDo();
        CachedBeanCopier.copy(narcoticDoctorRecordVo, narcoticDoctorRecordDo);

        NarcoticDoctorRecordDo narcoticDoctorRecordOldDo = narcoticDoctorRecordMapper.getNarcoticDoctorRecordById(narcoticDoctorRecordDo.getDoctorRecordId());
        if (narcoticDoctorRecordOldDo == null) {
            return new ResultVo<NarcoticDoctorRecordVo>(ServerStatusEnum.NARCOTIC_DOCTORRECORD_EMPTY);
        } else {
            if (narcoticDoctorRecordOldDo.getIsAudit() != 0 && narcoticDoctorRecordDo.getIsAudit() != 3) {
                return new ResultVo<NarcoticDoctorRecordVo>(ServerStatusEnum.NARCOTIC_DOCTORRECORD_AUDITED);
            }
        }

        NarcoticPatientDrugsRecordDo patientDrugsRecordDo = narcoticDrugsRecordMapper.getNarcoticPatientDrugsRecordById(narcoticDoctorRecordOldDo.getPatientDrugsRecordId());
        if (patientDrugsRecordDo == null) {
            return new ResultVo<NarcoticDoctorRecordVo>(ServerStatusEnum.NARCOTIC_DRUGSRECORD_EMPTY);
        }
        // 申请药品记录
        patientDrugsRecordDo.setAuditTime(date);
        if (narcoticDoctorRecordDo.getIsAudit() == 1 || narcoticDoctorRecordDo.getIsAudit() == 3) {
            patientDrugsRecordDo.setStatus(1);
            narcoticDoctorRecordDo.setIsAudit(1);
            patientDrugsRecordDo.setDrugsId(narcoticDoctorRecordDo.getDrugsId());
        } else {
            patientDrugsRecordDo.setStatus(5);
            if ("".equals(narcoticDoctorRecordDo.getRefuseReason())) {
                return new ResultVo<NarcoticDoctorRecordVo>(ServerStatusEnum.NARCOTIC_DOCTORRECORD_REASON_EMPTY);
            }
        }
        // 医生药品记录
        narcoticDoctorRecordDo.setAuditTime(date);
        // 药师药品记录信息
        if (narcoticDoctorRecordDo.getIsAudit() == 1) {
            NarcoticChemistRecordDo chemistRecordDo = narcoticChemistRecordMapper.selectChemistRecordByPatientDrugsRecordId(narcoticDoctorRecordOldDo.getPatientDrugsRecordId());
            if (null == chemistRecordDo) {
                NarcoticChemistRecordDo narcoticChemistRecordDo = new NarcoticChemistRecordDo();
                narcoticChemistRecordDo.setCreateTime(date);
                narcoticChemistRecordDo.setDoctorUserId(narcoticDoctorRecordDo.getDoctorUserId());
                narcoticChemistRecordDo.setHospitalId(narcoticDoctorRecordOldDo.getHospitalId());
                narcoticChemistRecordDo.setIsAudit(0);
                narcoticChemistRecordDo.setPatientDrugsRecordId(narcoticDoctorRecordOldDo.getPatientDrugsRecordId());
                narcoticChemistRecordDo.setDrugsId(narcoticDoctorRecordDo.getDrugsId());
                narcoticChemistRecordDo.setPatientInfoId(narcoticDoctorRecordOldDo.getPatientInfoId());
                narcoticChemistRecordDo.setPatientUserId(narcoticDoctorRecordOldDo.getPatientUserId());
                narcoticChemistRecordDo.setDoctorRecordId(narcoticDoctorRecordOldDo.getDoctorRecordId());
                narcoticChemistRecordDo.setStatus(1);
                narcoticChemistRecordMapper.insert(narcoticChemistRecordDo);
            } else {
                chemistRecordDo.setIsAudit(0);
                narcoticChemistRecordMapper.updateByPrimaryKey(chemistRecordDo);
            }
        }

        narcoticDrugsRecordMapper.updateByPrimaryKey(patientDrugsRecordDo);
        narcoticDoctorRecordMapper.updateByPrimaryKey(narcoticDoctorRecordDo);
        CachedBeanCopier.copy(narcoticDoctorRecordDo, narcoticDoctorRecordVo);
        return new ResultVo<NarcoticDoctorRecordVo>(narcoticDoctorRecordVo);
    }

    @Override
    public NarcoticChemistRecordVo getNarcoticChemistRecordById(Long chemistRecordId) {
        NarcoticChemistRecordDo narcoticChemistRecordDo = narcoticChemistRecordMapper.getNarcoticChemistRecordById(chemistRecordId);
        NarcoticChemistRecordVo narcoticChemistRecordVo = new NarcoticChemistRecordVo();
        CachedBeanCopier.copy(narcoticChemistRecordDo, narcoticChemistRecordVo);
        return narcoticChemistRecordVo;
    }

    /**
     * 药师药品记录 列表
     *
     * @param chemistUserId 药师用户ID
     * @param hospitalId    医疗机构ID
     * @param pageNo        页数
     * @param pageSize      条数
     * @param isPage        是否分页  0 否  1 是
     * @param searchName    关键字
     * @return
     */
    @Override
    public List<NarcoticChemistRecordVo> selectNarcoticChemistRecordList(Long chemistUserId, Long hospitalId, Integer pageNo, Integer pageSize, Integer isPage, String searchName) {
        Map<String, Object> map = new HashMap<String, Object>();
        if (isPage == 1) {
            map.put("pageNo", pageNo * pageSize);
            map.put("pageSize", pageSize);
        }
        map.put("chemistUserId", chemistUserId);
        map.put("hospitalId", hospitalId);
        map.put("isPage", isPage);
        map.put("searchName", searchName);
        List<NarcoticChemistRecordDo> narcoticChemistRecordDos = narcoticChemistRecordMapper.selectNarcoticChemistRecord(map);
        List<NarcoticChemistRecordVo> narcoticChemistRecordVos = narcoticChemistRecordDos.stream().map(p -> {
            NarcoticChemistRecordVo narcoticChemistRecordVo = new NarcoticChemistRecordVo();
            CachedBeanCopier.copy(p, narcoticChemistRecordVo);
            return narcoticChemistRecordVo;
        }).collect(Collectors.toList());
        return narcoticChemistRecordVos;
    }

    /**
     * 药师审核
     * 修改药师信息 状态和审核时间
     * 1 审核通过
     * 修改医生药品记录 状态 和 审核时间
     * 添加取药记录信息
     * 2 审核拒绝
     * 添加拒绝原因
     */
    @Override
    public ResultVo<NarcoticChemistRecordVo> auditNarcoticChemistRecord(NarcoticChemistRecordVo narcoticChemistRecordVo) {
        Date date = new Date();
        NarcoticChemistRecordDo narcoticChemistRecordDo = new NarcoticChemistRecordDo();
        CachedBeanCopier.copy(narcoticChemistRecordVo, narcoticChemistRecordDo);

        NarcoticChemistRecordDo narcoticChemistRecordOldDo = narcoticChemistRecordMapper.getNarcoticChemistRecordById(narcoticChemistRecordDo.getChemistRecordId());
        if (narcoticChemistRecordOldDo == null) {
            return new ResultVo<NarcoticChemistRecordVo>(ServerStatusEnum.NARCOTIC_CHEMISTRECORD_EMPTY);
        } else {
            if (narcoticChemistRecordOldDo.getIsAudit() != 0) {
                return new ResultVo<NarcoticChemistRecordVo>(ServerStatusEnum.NARCOTIC_CHEMISTRECORD_AUDITED);
            }
        }

        NarcoticPatientDrugsRecordDo patientDrugsRecordDo = narcoticDrugsRecordMapper.getNarcoticPatientDrugsRecordById(narcoticChemistRecordOldDo.getPatientDrugsRecordId());
        if (patientDrugsRecordDo == null) {
            return new ResultVo<NarcoticChemistRecordVo>(ServerStatusEnum.NARCOTIC_DRUGSRECORD_EMPTY);
        }

        // 申请药品记录 通过
        if (narcoticChemistRecordDo.getIsAudit() == 1) {
            patientDrugsRecordDo.setAuditTime(date);
            patientDrugsRecordDo.setStatus(2);
        } else { // 拒绝
            patientDrugsRecordDo.setStatus(0);
            if ("".equals(narcoticChemistRecordDo.getRefuseReason())) {
                return new ResultVo<NarcoticChemistRecordVo>(ServerStatusEnum.NARCOTIC_CHEMISTRECORD_REASON_EMPTY);
            }
        }

        // 药师药品记录
        narcoticChemistRecordDo.setAuditTime(date);
        NarcoticDoctorRecordDo takeDestroyDo = new NarcoticDoctorRecordDo();
        // 药师药品记录信息
        if (narcoticChemistRecordDo.getIsAudit() == 1) {

           /* NarcoticTakeDestroyDo TakeDestroyDo = narcoticTakeDestroyMapper.selectNarcoticTakeDestroyByPatientDrugsRecordId(narcoticChemistRecordOldDo.getPatientDrugsRecordId());
            if(TakeDestroyDo == null){

            } else {
                //TakeDestroyDo.set
            }*/

            NarcoticTakeDestroyDo narcoticTakeDestroyDo = new NarcoticTakeDestroyDo();
            narcoticTakeDestroyDo.setPatientUserId(narcoticChemistRecordOldDo.getPatientUserId());
            narcoticTakeDestroyDo.setHospitalId(narcoticChemistRecordOldDo.getHospitalId());
            narcoticTakeDestroyDo.setDrugsId(narcoticChemistRecordOldDo.getDrugsId());
            narcoticTakeDestroyDo.setPatientDrugsRecordId(narcoticChemistRecordOldDo.getPatientDrugsRecordId());
            narcoticTakeDestroyDo.setDoctorRecordId(narcoticChemistRecordOldDo.getDoctorRecordId());
            narcoticTakeDestroyDo.setPatientInfoId(narcoticChemistRecordOldDo.getPatientInfoId());
            narcoticTakeDestroyDo.setState(0);
            narcoticTakeDestroyDo.setChemistRecordId(narcoticChemistRecordDo.getChemistRecordId());
            narcoticTakeDestroyDo.setTakeMedicineAddress(narcoticChemistRecordVo.getTakeMedicineAddress());

            narcoticTakeDestroyDo.setTakeMedicineSetTime(narcoticChemistRecordVo.getTakeMedicineSetTime());
            narcoticTakeDestroyDo.setCreateTime(date);
            narcoticTakeDestroyDo.setStatus(1);
            narcoticTakeDestroyMapper.insert(narcoticTakeDestroyDo);

        } else if (narcoticChemistRecordDo.getIsAudit() == 2) {
            takeDestroyDo = narcoticDoctorRecordMapper.selectNarcoticDoctorRecordByNarcoticDrugsRecordId(narcoticChemistRecordOldDo.getPatientDrugsRecordId());
            takeDestroyDo.setIsAudit(0);
            narcoticDoctorRecordMapper.updateByPrimaryKey(takeDestroyDo);
        }

        narcoticDrugsRecordMapper.updateByPrimaryKey(patientDrugsRecordDo);
        narcoticChemistRecordMapper.updateByPrimaryKey(narcoticChemistRecordDo);
        CachedBeanCopier.copy(narcoticChemistRecordOldDo, narcoticChemistRecordDo);
        CachedBeanCopier.copy(narcoticChemistRecordDo, narcoticChemistRecordVo);
        narcoticChemistRecordVo.setTakeMedicineSetTime(narcoticChemistRecordVo.getTakeMedicineSetTime());
        narcoticChemistRecordVo.setTakeMedicineAddress(narcoticChemistRecordVo.getTakeMedicineAddress());

        if (narcoticChemistRecordDo.getIsAudit() == 2) {
            try {
                UserDo chemistUser = userClient.getUser(narcoticChemistRecordDo.getChemistUserId());
                UserDo patientUser = userClient.getUser(narcoticChemistRecordOldDo.getPatientUserId());

                Map<String, String> smsMap = new HashMap<String, String>();
                smsMap.put("refusal_cause", narcoticChemistRecordDo.getRefuseReason());
                smsMap.put("pharmacist_name", chemistUser.getName());
                smsMap.put("pharmacist_phone", chemistUser.getMobile());
                smsMap.put("name", patientUser.getUsername());
                smsMap.put("drug_name", patientDrugsRecordDo.getDrugsName());
                JSMSUtil.sendTempSMS(takeDestroyDo.getDoctorPhone(), 183587, smsMap);
            } catch (Exception e) {
                log.error(e.getMessage(), e);
            }
        }
        return new ResultVo<NarcoticChemistRecordVo>(narcoticChemistRecordVo);
    }

    /**
     * 药师重置审核
     */
    @Override
    public Long resetAuditNarcoticChemistRecord(Long chemistRecordId, Long chemistUserId) {
        Date date = new Date();
        NarcoticChemistRecordDo narcoticChemistRecordDo = new NarcoticChemistRecordDo();

        NarcoticChemistRecordDo narcoticChemistRecordOldDo = narcoticChemistRecordMapper.getNarcoticChemistRecordById(chemistRecordId);
        if (narcoticChemistRecordOldDo == null) {
            throw new BaseRuntimeException(ServerStatusEnum.NARCOTIC_CHEMISTRECORD_EMPTY);
        }

        NarcoticPatientDrugsRecordDo patientDrugsRecordDo = narcoticDrugsRecordMapper.getNarcoticPatientDrugsRecordById(narcoticChemistRecordOldDo.getPatientDrugsRecordId());
        if (patientDrugsRecordDo == null) {
            throw new BaseRuntimeException(ServerStatusEnum.NARCOTIC_DRUGSRECORD_EMPTY);
        }

        NarcoticTakeDestroyDo narcoticTakeDestroyDo = narcoticTakeDestroyMapper.selectNarcoticTakeDestroyByPatientDrugsRecordId(patientDrugsRecordDo.getPatientDrugsRecordId());
        if (narcoticTakeDestroyDo == null) {
            throw new BaseRuntimeException(ServerStatusEnum.NARCOTIC_TAKEDESTROY_AUDITED);
        }

        narcoticTakeDestroyDo.setStatus(0);
        narcoticTakeDestroyMapper.updateByPrimaryKey(narcoticTakeDestroyDo);

        patientDrugsRecordDo.setStatus(1);
        narcoticDrugsRecordMapper.updateByPrimaryKey(patientDrugsRecordDo);

        narcoticChemistRecordOldDo.setIsAudit(0);
        return narcoticChemistRecordMapper.updateByPrimaryKey(narcoticChemistRecordOldDo);
    }

    /**
     * 取药销毁记录 信息
     *
     * @param takeDestroyId 取药记录id
     * @return
     */
    @Override
    public NarcoticTakeDestroyVo getNarcoticTakeDestroyById(Long takeDestroyId) {
        NarcoticTakeDestroyDo narcoticTakeDestroyDo = narcoticTakeDestroyMapper.getNarcoticTakeDestroyById(takeDestroyId);
        NarcoticTakeDestroyVo narcoticTakeDestroyVo = new NarcoticTakeDestroyVo();
        CachedBeanCopier.copy(narcoticTakeDestroyDo, narcoticTakeDestroyVo);
        return narcoticTakeDestroyVo;
    }

    @Override
    public List<NarcoticTakeDestroyVo> selectNarcoticTakeDestroy(Long hospitalId, String type, Integer pageNo, Integer pageSize, Integer isPage, String searchName) {
        Map<String, Object> map = new HashMap<String, Object>();
        if (isPage == 1) {
            map.put("pageNo", pageNo * pageSize);
            map.put("pageSize", pageSize);
        }
        map.put("hospitalId", hospitalId);
        map.put("type", type);
        map.put("isPage", isPage);
        map.put("searchName", searchName);

        List<NarcoticTakeDestroyDo> narcoticTakeDestroyDos = narcoticTakeDestroyMapper.selectNarcoticTakeDestroy(map);
        List<NarcoticTakeDestroyVo> narcoticTakeDestroyVs = narcoticTakeDestroyDos.stream().map(p -> {
            NarcoticTakeDestroyVo narcoticTakeDestroyVo = new NarcoticTakeDestroyVo();
            CachedBeanCopier.copy(p, narcoticTakeDestroyVo);
            return narcoticTakeDestroyVo;
        }).collect(Collectors.toList());
        return narcoticTakeDestroyVs;
    }

    /**
     * 患者取药
     *
     * @param takeDestroyDo 取药入参
     * @return
     */
    @Override
    public ResultVo<NarcoticTakeDestroyVo> auditNarcoticTakeMedicine(TakeDestroyDo takeDestroyDo) {
        Date date = new Date();
        NarcoticTakeDestroyDo narcoticTakeDestroyDo = narcoticTakeDestroyMapper.getNarcoticTakeDestroyById(takeDestroyDo.getTakeDestroyId());

        if (1 == narcoticTakeDestroyDo.getState()) {
            return new ResultVo<NarcoticTakeDestroyVo>(ServerStatusEnum.NARCOTIC_TAKEMEDICINE_TAKEED);
        }

        NarcoticPatientDrugsRecordDo patientDrugsRecordDo = narcoticDrugsRecordMapper.getNarcoticPatientDrugsRecordById(narcoticTakeDestroyDo.getPatientDrugsRecordId());
        if (patientDrugsRecordDo == null) {
            return new ResultVo<NarcoticTakeDestroyVo>(ServerStatusEnum.NARCOTIC_DRUGSRECORD_EMPTY);
        }

        narcoticTakeDestroyDo.setTakeMedicineFile(takeDestroyDo.getTakeMedicineFile());
        narcoticTakeDestroyDo.setBatchNo(takeDestroyDo.getBatchNo());
        narcoticTakeDestroyDo.setValidTime(takeDestroyDo.getValidTime());
        narcoticTakeDestroyDo.setSendMedicineUserId(takeDestroyDo.getSendMedicineUserId());
        narcoticTakeDestroyDo.setRecycleNum(takeDestroyDo.getRecycleNum());
        narcoticTakeDestroyDo.setRecycleRemark(takeDestroyDo.getRecycleRemark());
        narcoticTakeDestroyDo.setDestroyFile(takeDestroyDo.getDestroyFile());
        narcoticTakeDestroyDo.setDrugsFactoryId(takeDestroyDo.getDrugsFactoryId());
        narcoticTakeDestroyDo.setDestroyTime(date);
        narcoticTakeDestroyDo.setTakeMedicineConfimTime(date);
        narcoticTakeDestroyDo.setState(1);
        narcoticTakeDestroyMapper.updateByPrimaryKey(narcoticTakeDestroyDo);

        patientDrugsRecordDo.setAuditTime(date);
        patientDrugsRecordDo.setStatus(3);
        narcoticDrugsRecordMapper.updateByPrimaryKey(patientDrugsRecordDo);
        NarcoticTakeDestroyVo narcoticTakeDestroyVo = new NarcoticTakeDestroyVo();
        CachedBeanCopier.copy(narcoticTakeDestroyDo, narcoticTakeDestroyVo);
        return new ResultVo<NarcoticTakeDestroyVo>(narcoticTakeDestroyVo);
    }

    @Override
    public ResultVo<NarcoticTakeDestroyVo> auditNarcoticDestroy(NarcoticTakeDestroyVo narcoticTakeDestroyVo) {

        /**
         *
         * 销毁
         *   修改销毁数据
         *   修改申请用药数据 状态 4
         */
        Date date = new Date();
        NarcoticTakeDestroyDo narcoticTakeDestroyDo = new NarcoticTakeDestroyDo();
        CachedBeanCopier.copy(narcoticTakeDestroyVo, narcoticTakeDestroyDo);

        NarcoticTakeDestroyDo narcoticTakeDestroyOldDo = narcoticTakeDestroyMapper.getNarcoticTakeDestroyById(narcoticTakeDestroyVo.getTakeDestroyId());
        if (2 == narcoticTakeDestroyOldDo.getState()) {
            return new ResultVo<NarcoticTakeDestroyVo>(ServerStatusEnum.NARCOTIC_TAKEDESTORY_ED);
        }

        NarcoticPatientDrugsRecordDo patientDrugsRecordDo = narcoticDrugsRecordMapper.getNarcoticPatientDrugsRecordById(narcoticTakeDestroyOldDo.getPatientDrugsRecordId());
        if (patientDrugsRecordDo == null) {
            return new ResultVo<NarcoticTakeDestroyVo>(ServerStatusEnum.NARCOTIC_DRUGSRECORD_EMPTY);
        }

        narcoticTakeDestroyDo.setDestroyTime(date);
        narcoticTakeDestroyDo.setState(2);
        narcoticTakeDestroyMapper.updateByPrimaryKey(narcoticTakeDestroyDo);

        patientDrugsRecordDo.setAuditTime(date);
        patientDrugsRecordDo.setStatus(4);
        narcoticDrugsRecordMapper.updateByPrimaryKey(patientDrugsRecordDo);

        CachedBeanCopier.copy(narcoticTakeDestroyOldDo, narcoticTakeDestroyDo);
        CachedBeanCopier.copy(narcoticTakeDestroyDo, narcoticTakeDestroyVo);
        narcoticTakeDestroyVo.setDestroyTime(date);
        narcoticTakeDestroyVo.setState(2);
        return new ResultVo<NarcoticTakeDestroyVo>(narcoticTakeDestroyVo);
    }

    @Override
    public ResultVo<NarcoticPatientInfoVo> auditNarcoticPatientInfo(NarcoticPatientInfoVo narcoticPatientInfoVo) {
        NarcoticPatientInfoDo narcoticPatientInfoDo = new NarcoticPatientInfoDo();
        Date date = new Date();

        NarcoticPatientInfoDo narcoticPatientInfoOldDo = narcoticPatientInfoMapper.getNarcoticPatientInfoById(narcoticPatientInfoVo.getPatientInfoId());
        if (narcoticPatientInfoOldDo == null) {
            return new ResultVo<NarcoticPatientInfoVo>(ServerStatusEnum.NARCOTIC_USERINFO_EMPTY);
        }
        if (narcoticPatientInfoOldDo.getIsAudit() != 0) {
            return new ResultVo<NarcoticPatientInfoVo>(ServerStatusEnum.NARCOTIC_USERINFO_AUDITED);
        }
        if (narcoticPatientInfoVo.getIsAudit() == 2) {
            if ("".equals(narcoticPatientInfoVo.getRefuseReason())) {
                return new ResultVo<NarcoticPatientInfoVo>(ServerStatusEnum.NARCOTIC_USERINFO_REASON_EMPTY);
            }
        }
        narcoticPatientInfoDo.setPatientInfoId(narcoticPatientInfoVo.getPatientInfoId());
        narcoticPatientInfoDo.setIsAudit(narcoticPatientInfoVo.getIsAudit());
        narcoticPatientInfoDo.setRefuseReason(narcoticPatientInfoVo.getRefuseReason());
        narcoticPatientInfoDo.setDiagnose(narcoticPatientInfoVo.getDiagnose());
        narcoticPatientInfoDo.setAuditTime(date);
        narcoticPatientInfoDo.setValidTime(DateUtils.addMonths(date, 3));
        narcoticPatientInfoDo.setAuditUserId(narcoticPatientInfoVo.getAuditUserId());
        narcoticPatientInfoMapper.updateByPrimaryKey(narcoticPatientInfoDo);

        try {
            UserDo user = userClient.getUser(narcoticPatientInfoVo.getAuditUserId());
            String is_audit;
            if (narcoticPatientInfoVo.getIsAudit() == 1) {
                is_audit = "审核通过";
            } else {
                is_audit = "驳回";
            }

            Map<String, String> smsMap = new HashMap<String, String>();
            smsMap.put("status", is_audit);
            smsMap.put("name", user.getName());
            smsMap.put("phone", user.getMobile());
            JSMSUtil.sendTempSMS(narcoticPatientInfoOldDo.getMobile(), 182685, smsMap);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }

        CachedBeanCopier.copy(narcoticPatientInfoDo, narcoticPatientInfoVo);
        return new ResultVo<NarcoticPatientInfoVo>(narcoticPatientInfoVo);
    }

    @Override
    public List<NarcoticPatientInfoVo> selectNarcoticPatientInfoPageByHospital(Long hospitalId, Integer pageNo, Integer pageSize, Integer isPage, String searchName) {
        Map<String, Object> map = new HashMap<String, Object>();
        if (isPage == 1) {
            map.put("pageNo", pageNo * pageSize);
            map.put("pageSize", pageSize);
        }
        map.put("searchName", searchName);
        map.put("hospitalId", hospitalId);
        map.put("isPage", isPage);
        List<NarcoticPatientInfoDo> narcoticUserInfoDos = narcoticPatientInfoMapper.selectNarcoticPatientInfoDo(map);
        List<NarcoticPatientInfoVo> varcoticUserInfoVos = narcoticUserInfoDos.stream().map(p -> {
            NarcoticPatientInfoVo narcoticUserInfoVo = new NarcoticPatientInfoVo();
            CachedBeanCopier.copy(p, narcoticUserInfoVo);
            return narcoticUserInfoVo;
        }).collect(Collectors.toList());
        return varcoticUserInfoVos;
    }

    /**
     * 药品跟踪记录--详情
     *
     * @param patientDrugsRecordId 申请用药记录ID
     * @return NarcoticDrugsRecordDo
     */
    @Override
    public ResultVo<NarcoticPatientDrugsRecordDto> getNarcoticPatientDrugsRecordDtoById(Long patientDrugsRecordId) {
        //用户信息
        NarcoticPatientDrugsRecordDto patientDrugsRecordDto = narcoticDrugsRecordMapper.getNarcoticPatientDrugsRecordDtoById(patientDrugsRecordId);
        if (patientDrugsRecordDto == null) {
            return new ResultVo<NarcoticPatientDrugsRecordDto>(ServerStatusEnum.NARCOTIC_DRUGSRECORD_EMPTY);
        }
        //资料信息
        NarcoticPatientInfoVo narcoticPatientInfoVo = getNarcoticPatientInfoById(patientDrugsRecordDto.getPatientInfoId());
        if (narcoticPatientInfoVo != null) {
            patientDrugsRecordDto.setPatientInfoVo(narcoticPatientInfoVo);
        }

        //代领人信息
        NarcoticAgentInfoDo narcoticAgentInfoDo = narcoticAgentInfoMapper.selectNarcoticAgentInfo(patientDrugsRecordDto.getPatientInfoId());
        if (null != narcoticAgentInfoDo) {
            NarcoticAgentInfoVo narcoticAgentInfoVo = new NarcoticAgentInfoVo();
            CachedBeanCopier.copy(narcoticAgentInfoDo, narcoticAgentInfoVo);
            patientDrugsRecordDto.setAgentInfoVo(narcoticAgentInfoVo);
        }

        //医生记录信息
        NarcoticDoctorRecordDo narcoticDoctorRecordDo = narcoticDoctorRecordMapper.selectNarcoticDoctorRecordByNarcoticDrugsRecordId(patientDrugsRecordDto.getPatientDrugsRecordId());
        if (null != narcoticDoctorRecordDo) {
            NarcoticDoctorRecordVo narcoticDoctorRecordVo = new NarcoticDoctorRecordVo();
            CachedBeanCopier.copy(narcoticDoctorRecordDo, narcoticDoctorRecordVo);

            List<NarcoticDoctorRecordVo> narcoticDoctorRecordVos = new ArrayList<NarcoticDoctorRecordVo>();
            narcoticDoctorRecordVos.add(narcoticDoctorRecordVo);
            patientDrugsRecordDto.setDoctorRecordVos(narcoticDoctorRecordVos);
        }

        //药师记录信息
        NarcoticChemistRecordDo narcoticChemistRecordDo = narcoticChemistRecordMapper.selectChemistRecordByPatientDrugsRecordId(patientDrugsRecordDto.getPatientDrugsRecordId());
        if (null != narcoticChemistRecordDo) {
            NarcoticChemistRecordVo narcoticChemistRecordVo = new NarcoticChemistRecordVo();
            CachedBeanCopier.copy(narcoticChemistRecordDo, narcoticChemistRecordVo);

            List<NarcoticChemistRecordVo> narcoticChemistRecordVos = new ArrayList<NarcoticChemistRecordVo>();
            narcoticChemistRecordVos.add(narcoticChemistRecordVo);
            patientDrugsRecordDto.setChemistRecordVos(narcoticChemistRecordVos);
        }

        //取药销毁信息
        NarcoticTakeDestroyDo narcoticTakeDestroyDo = narcoticTakeDestroyMapper.selectNarcoticTakeDestroyByPatientDrugsRecordId(patientDrugsRecordDto.getPatientDrugsRecordId());
        if (narcoticTakeDestroyDo != null) {
            NarcoticTakeDestroyVo narcoticTakeDestroyVo = new NarcoticTakeDestroyVo();
            CachedBeanCopier.copy(narcoticTakeDestroyDo, narcoticTakeDestroyVo);
            //销毁附件
            if (narcoticTakeDestroyVo.getDestroyFile() != null) {
                List<FileLogVo> fileLogList = narcoticPatientInfoMapper.selectFileByCode(narcoticTakeDestroyVo.getDestroyFile());
                if (fileLogList != null) {
                    narcoticTakeDestroyVo.setDestroyFileList(fileLogList);
                }
            }
            //取药附件
            if (narcoticTakeDestroyVo.getTakeMedicineFile() != null) {
                List<FileLogVo> fileLogList = narcoticPatientInfoMapper.selectFileByCode(narcoticTakeDestroyVo.getTakeMedicineFile());
                if (fileLogList != null) {
                    narcoticTakeDestroyVo.setTakeMedicineFileList(fileLogList);
                }
            }
            patientDrugsRecordDto.setTakeDestroyVo(narcoticTakeDestroyVo);
        }
        return new ResultVo<NarcoticPatientDrugsRecordDto>(patientDrugsRecordDto);
    }

    /**
     * 药品厂家列表
     *
     * @param hospitalId 医院ID
     * @return
     */
    @Override
    public List<NarcoticDrugsFactoryVo> getNarcoticDrugsFactoryList(@RequestParam("hospitalId") Long hospitalId) {
        return narcoticDrugsFactoryMapper.getNarcoticDrugsFactoryList(hospitalId);
    }

    /**
     * 扫码验证药品
     *
     * @param patientDrugsRecordId 申请用药记录Id
     * @param patientUserId        患者用户Id
     * @return
     */
    @Override
    public QRCodeProDrugsVo scanDrugsQRCode(@RequestParam("patientDrugsRecordId") Long patientDrugsRecordId,
                                            @RequestParam("patientUserId") Long patientUserId) {
        return narcoticDoctorRecordMapper.scanDrugsQRCode(patientDrugsRecordId, patientUserId);
    }

    /**
     * 获取药品统计数据
     *
     * @param userId   用户ID
     * @param dateTime 时间节点
     * @return
     */
    @Override
    public DrugsCountResultVo getDrugsCountResultVo(Long userId, Date dateTime) {
        DrugsCountResultVo drugsCountResultVo = new DrugsCountResultVo();

        List<DrugsCountVo> drugsCountZXList = narcoticDrugsMapper.getDrugsCountZX(dateTime);
        drugsCountResultVo.setDrugsCountZXList(drugsCountZXList);

        List<DrugsCountVo> drugsCountBTList = narcoticDrugsMapper.getDrugsCountBT(dateTime);
        drugsCountResultVo.setDrugsCountBTList(drugsCountBTList);
        return drugsCountResultVo;
    }

    /**
     * 获取用户有效资料
     *
     * @param userId 用户Id
     */
    @Override
    public List<NarcoticPatientInfoVo> getPatientInfoByUserId(Long userId) {
        return narcoticPatientInfoMapper.getPatientInfoByUserId(userId);
    }

    /**
     * 获取用户未完成的用药申请列表
     *
     * @param userId 用户Id
     */
    @Override
    public List<NarcoticPatientDrugsRecordVo> getPatientDrugsRecordByUserId(Long userId) {
        return narcoticDrugsRecordMapper.getPatientDrugsRecordByUserId(userId, null);
    }

    /**
     * 获取用户有效资料
     */
    @Override
    public void timePatientInfoByUserId() {
        List<NarcoticPatientInfoDo> narcoticPatientInfoDos = narcoticPatientInfoMapper.timePatientInfoByUserId();
        if (!CollectionUtils.isEmpty(narcoticPatientInfoDos)) {
            for (NarcoticPatientInfoDo narcoticPatientInfoVo : narcoticPatientInfoDos) {
                try {
                    Map<String, String> smsMap = new HashMap<String, String>();
                    JSMSUtil.sendTempSMS(narcoticPatientInfoVo.getMobile(), 182895, smsMap);
                } catch (Exception e) {
                    log.error(e.getMessage(), e);
                }
            }
        }
    }

    /**
     * 用户申请定时过期
     */
    @Override
    public void timeUpdateDrugsRecordStatus() {
        Date date = new Date();
        List<NarcoticPatientDrugsRecordDo> drugsRecordDoList = narcoticDrugsRecordMapper.timeUpdateDrugsRecordStatus(com.secxiot.common.utils.DateUtils.nextDay(-2));
        if (!CollectionUtils.isEmpty(drugsRecordDoList)) {
            for (NarcoticPatientDrugsRecordDo drugsRecordDo : drugsRecordDoList) {
                drugsRecordDo.setStatus(6);
                drugsRecordDo.setAuditTime(date);
                narcoticDrugsRecordMapper.updateByPrimaryKey(drugsRecordDo);

                NarcoticDoctorRecordDo narcoticDoctorRecordDo = narcoticDoctorRecordMapper.selectNarcoticDoctorRecordByNarcoticDrugsRecordId(drugsRecordDo.getPatientDrugsRecordId());
                narcoticDoctorRecordDo.setIsAudit(4);
                narcoticDoctorRecordDo.setAuditTime(date);
                narcoticDoctorRecordMapper.updateByPrimaryKey(narcoticDoctorRecordDo);
            }
        }
    }
}
